Use smart pointers over xmalloc and manual life management (#833)
authorGPSBabel <12013583+GPSBabelDeveloper@users.noreply.github.com>
Sun, 6 Feb 2022 22:24:56 +0000 (16:24 -0600)
committerGitHub <noreply@github.com>
Sun, 6 Feb 2022 22:24:56 +0000 (16:24 -0600)
* Use reference counted pointers instead of managing our own.
Fixes a leak when using debug printing.

garmin_gpi.cc

index 123d08dcf6e95c8b160363d756edb6b087ff8906..74fc07b333a6491fb5e67f452ce1e8d850159093 100644 (file)
@@ -726,9 +726,9 @@ read_tag(const char* caller, const int tag, Waypoint* wpt)
 #ifdef GPI_DBG
   {
     int x;
-    unsigned char* b = (unsigned char*) xmalloc(sz);
+    std::unique_ptr<unsigned char[]> b(new unsigned char(sz));
     fprintf(stderr, "Tag: %x\n", tag);
-    gbfread(b, 1, sz, fin);
+    gbfread(b.get(), 1, sz, fin);
     fprintf(stderr, "\n");
     for (x = 0; x < sz; x++) {
       fprintf(stderr, "%02x ", b[x]);
@@ -1267,7 +1267,6 @@ load_bitmap_from_file(const char* fname, unsigned char** data, int* data_sz)
   int dest_bpp;
   int src_line_sz, dest_line_sz;
   bmp_header_t src_h;
-  uint32_t* color_table = nullptr;
   gpi_bitmap_header_t* dest_h;
   unsigned char* ptr;
 
@@ -1326,9 +1325,10 @@ load_bitmap_from_file(const char* fname, unsigned char** data, int* data_sz)
     fatal(MYNAME ": Sorry, we don't support compressed bitmaps.\n");
   }
 
+  std::unique_ptr<uint32_t[]> color_table;
   if (src_h.used_colors > 0) {
-    color_table = new uint32_t[src_h.used_colors];
-    gbfread(color_table, 1, 4 * src_h.used_colors, f);
+    color_table.reset(new uint32_t[src_h.used_colors]);
+    gbfread(color_table.get(), 1, 4 * src_h.used_colors, f);
     for (i = 0; i < src_h.used_colors; i++) {
       uint32_t color = color_table[i];
       /* swap blue and red value */
@@ -1416,7 +1416,6 @@ load_bitmap_from_file(const char* fname, unsigned char** data, int* data_sz)
   if (bytesout != *data_sz) {
     warning(MYNAME ": Code error in load_bitmap_from_file, expected output size %d, actual output %td.", *data_sz, bytesout);
   }
-  delete[] color_table;
   gbfclose(f);
 }